package com.yunchao.feature.business.domain.model

import com.yunchao.feature.business.domain.enum.BusinessType


internal data class BusinessInfoModel(
    val tranTypeID: String, //    String 17 N  交易代码
    val tranID: String,//   String 13 N  业务序号
    val chkUserId: String,//    String 19 Y  复核人id
    val sequency: String,// String 50 N  交易流水号
    val giveDataTime: String,// String 14 N  交易时间
    val giveOperator: String,// String 19 N  操作人id
    val cardID: String,//   String 20 Y  操作人卡号
    val packInfoList: List<BusinessPackModel>,
    val busiInfoList: List<BusinessPackModel>,
    val packInfoPayloads: List<BusinessPackModel>,
)

internal fun BusinessInfoModel.getPackInfoMap(): Map<String, BusinessPackModel> {
    return if (BusinessType.BusinessTypeMIMISIF001.type == tranTypeID) {
        packInfoPayloads.mapNotNull { packInfo ->
            packInfo.lockEPCInfo?.id?.let { id ->
                id to packInfo
            }
        }.toMap()
    } else {
        packInfoList.filter { it.sackNo.isNotBlank() }
            .associateBy { it.sackNo }
            .toMutableMap()
            .apply {
                packInfoPayloads.forEach { packInfo ->
                    packInfo.lockEPCInfo?.id?.let { id ->
                        remove(id)
                    }
                }
            }
    }
}


internal fun BusinessInfoModel.getTranTypeName(): String = when (tranTypeID) {
    BusinessType.BusinessTypeMIMISIF001.type -> BusinessType.BusinessTypeMIMISIF001.describe
    BusinessType.BusinessTypeMIMISIF003.type -> BusinessType.BusinessTypeMIMISIF003.describe
    BusinessType.BusinessTypeMIMISIF005.type -> BusinessType.BusinessTypeMIMISIF005.describe
    BusinessType.BusinessTypeMIMISIF007.type -> BusinessType.BusinessTypeMIMISIF007.describe
    else -> ""
}
